package com.cuitrip.im;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.FragmentActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.im.model.CTInformationNotificationMessage;
import com.cuitrip.service.R;
import com.cuitrip.util.time.InputTime;
import com.cuitrip.util.time.OutputTime;
import com.cuitrip.util.time.TimeUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import io.rong.imkit.RongContext;
import io.rong.imkit.RongIM;
import io.rong.imkit.model.ConversationKey;
import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.ArraysDialogFragment;
import io.rong.imkit.widget.provider.IContainerItemProvider;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.PublicServiceProfile;
import io.rong.imlib.model.UserInfo;

/**
 * Created by MiJack on 2016/1/29.
 */
@ProviderTag(
        messageContent = CTInformationNotificationMessage.class,
        showPortrait = false,
        showProgress = false,
        showWarning = false,
        centerInHorizontal = true,
        showSummaryWithName = false
)
public class CtInfoNotificationMsgItemProvider extends IContainerItemProvider.MessageProvider<CTInformationNotificationMessage> {


    public static CTInformationNotificationMessage build(String content, CtParamKey key, HashMap<String, String> map) {
        CTInformationNotificationMessage message = CTInformationNotificationMessage.obtain(content);
        CtParam ctParam = new CtParam(CtParam.ORDER_KEY, key.name(), map);
        message.setExtra(ctParam.toJSONString());
        return message;
    }

    public enum CtParamKey {
        BOOK_MSG_BOOKED(R.string.book_msg_booked, R.string.BOOK_MSG_BOOKED_PRE),
        BOOK_MSG_MODIFY_ONE(R.string.book_msg_modify_one, R.string.BOOK_MSG_MODIFY_ONE_PRE),
        BOOK_MSG_MODIFY_MORE(R.string.book_msg_modify_more, R.string.BOOK_MSG_MODIFY_MORE_PRE),
        BOOK_MSG_CONFRIMED(R.string.book_msg_confrimed, R.string.BOOK_MSG_CONFRIMED_PRE),
        BOOK_MSG_PAID(R.string.book_msg_paid, R.string.BOOK_MSG_PAID_PRE),
        BOOK_MSG_CANCELLED(R.string.book_msg_cancelled, R.string.BOOK_MSG_CANCELLED_PRE),
        BOOK_MSG_REVIEWED(R.string.book_msg_reviewed, R.string.BOOK_MSG_REVIEWED_PRE);

        public int stringRes;
        public int commonRes;//通用的res

        CtParamKey(int stringRes, int commonRes) {
            this.stringRes = stringRes;
            this.commonRes = commonRes;
        }


    }


    public void bindView(View v, int position, CTInformationNotificationMessage content, UIMessage message) {
        CtParam param = CtParam.parseObject(content.getExtra());
        try {
            if (param != null && CtParam.ORDER_KEY.equals(param.cuitrip) && param.replace != null) {
                CtParamKey paramKey = CtParamKey.valueOf(param.key);
                String messageStr = v.getResources().getString(paramKey.stringRes);
                for (Map.Entry<String, Object> entry : param.replace.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue().toString();
                    if (key.contains(CtParam.DATE_KEY)) {
                        String temp = TimeUtil.formatTime(
                                InputTime.build(value, TimeZone.getTimeZone("GMT+000")),
                                OutputTime.getInstance(OutputTime.OutputType.Type_DAY, TimeZone.getTimeZone("GMT+000")));
                        value = temp;
                    }
                    messageStr = messageStr.replace(key, value);
                }
                content.setContent(messageStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        CtInfoNotificationMsgItemProvider.ViewHolder viewHolder = (CtInfoNotificationMsgItemProvider.ViewHolder) v.getTag();
        if (content != null && !TextUtils.isEmpty(content.getContent())) {
            viewHolder.contentTextView.setText(content.getContent());
        }

    }

    public Spannable getContentSummary(CTInformationNotificationMessage data) {
        return data != null && !TextUtils.isEmpty(data.getContent()) ? new SpannableString(data.getContent()) : null;
    }

    public void onItemClick(View view, int position, CTInformationNotificationMessage content, UIMessage message) {
    }

    public void onItemLongClick(View view, int position, CTInformationNotificationMessage content, final UIMessage message) {
        String name = null;
        if (!message.getConversationType().getName().equals(Conversation.ConversationType.APP_PUBLIC_SERVICE.getName()) && !message.getConversationType().getName().equals(Conversation.ConversationType.PUBLIC_SERVICE.getName())) {
            UserInfo items2 = (UserInfo) RongContext.getInstance().getUserInfoCache().get(message.getSenderUserId());
            if (items2 != null) {
                name = items2.getName();
            }
        } else {
            ConversationKey items = ConversationKey.obtain(message.getTargetId(), message.getConversationType());
            PublicServiceProfile info = (PublicServiceProfile) RongContext.getInstance().getPublicServiceInfoCache().get(items.getKey());
            if (info != null) {
                name = info.getName();
            }
        }

        String[] items1 = new String[]{view.getContext().getResources().getString(io.rong.imkit.R.string.rc_dialog_item_message_delete)};
        ArraysDialogFragment.newInstance(name, items1).setArraysDialogItemListener(new ArraysDialogFragment.OnArraysDialogItemListener() {
            public void OnArraysDialogItemClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    RongIM.getInstance().getRongIMClient().deleteMessages(new int[]{message.getMessageId()}, (RongIMClient.ResultCallback) null);
                }

            }
        }).show(((FragmentActivity) view.getContext()).getSupportFragmentManager());
    }

    public View newView(Context context, ViewGroup group) {
        View view = LayoutInflater.from(context).inflate(io.rong.imkit.R.layout.rc_item_information_notification_message, (ViewGroup) null);
        CtInfoNotificationMsgItemProvider.ViewHolder viewHolder = new CtInfoNotificationMsgItemProvider.ViewHolder();
        viewHolder.contentTextView = (TextView) view.findViewById(io.rong.imkit.R.id.rc_msg);
        viewHolder.contentTextView.setAutoLinkMask(0);

        view.setTag(viewHolder);
        return view;
    }

    class ViewHolder {
        TextView contentTextView;

        ViewHolder() {
        }
    }


}
